﻿using System;
using System.Collections.Generic;

namespace ISR.Config {
	/// <summary>
	/// Class for saving user-defined config values.
	/// </summary>
	[Serializable]
	class UserConfig {
		/// <summary>
		/// The collection of user defined values.
		/// </summary>
		private Dictionary<string, object> _data = null;

		/// <summary>
		/// Flag whether some changes have been already applied.
		/// </summary>
		[NonSerialized]
		private bool _dirty = false;

		/// <summary>
		/// Initializes a new instance of the <see cref="UserConfig" /> class.
		/// </summary>
		public UserConfig() {
			_data = new Dictionary<string, object>();
		}

		/// <summary>
		/// Gets the option.
		/// </summary>
		/// <param name="name">The name of the option.</param>
		/// <returns>System.Object</returns>
		public object getOption(string name) {
			object o;

			if (_data.TryGetValue(name, out o)) {
				return o;
			}
			else {
				return null;
			}
		}

		/// <summary>
		/// Determines whether some changes have been already applied.
		/// </summary>
		/// <returns><c>true</c> if this instance has changes; <c>false</c> otherwise.</returns>
		public bool hasChanges() {
			return _dirty;
		}

		/// <summary>
		/// Sets the option.
		/// </summary>
		/// <param name="name">The option name.</param>
		/// <param name="value">The option value.</param>
		public void setOption(string name, object value) {
			if (_data.ContainsKey(name)) {
				_data.Remove(name);
			}

			_data.Add(name, value);
			_dirty = true;
		}

		/// <summary>
		/// Clears the dirty flag.
		/// </summary>
		public void unsetDirty() {
			_dirty = false;
		}
	}
}